﻿/* CommonComponent for MultiLine add .Net Framework 
 * 程序名称：多行选择子窗体DEMO示例隐藏代码类
 * 程序功能：实现在子窗体中选择多行或全选，并将值传递到主窗体。
 * 使用要点：
 * 一、窗体UI呈现
 * 1、复制*.aspx中的<html></html>间的代码
 * 2、修改绑定表字段，详见使用说明。
 * 二、窗体逻辑事件
 * 根据实际情况改变有"maybe"注释的代码
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonComponent.Common;

public partial class pages_Demo_rowEdit_ChildForm : System.Web.UI.Page
{

    #region 私有静态变量
    const string _VIEWSTATE_NEW_SELECTED = "mySelectedItems";
    const string _CTR_CHECKBOX_PKID = "chkPKID";
    const string _CTR_HIDDENFIELD_PKID = "hfdPKID";
    const string _CTR_TEXTBOX_PKID_SOURCE = "txtSource";    //maybe to edit数据表中主键字段名
    const string _TABLE_FIELD_PKID = "SID";   //maybe to edit数据表中主键字段名

    BMultiWindow bMultiChild = new BMultiWindow();
    #endregion


    #region 事件
    /// <summary>
    /// 页面载入事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //maybe to edit(设置子表主键名)
            string strSQL = "SELECT [SID],[SName],[SSex] FROM [demo_MultiLine_child]";  
            bMultiChild.PageLoadChild(ref GridView1,strSQL);
        }
    }

    /// <summary>
    /// GridView绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_DataBinding(object sender, EventArgs e)
    {
        //在每一次重新绑定之前，需要调用CollectSelected方法从当前页收集选中项的情况
        CollectSelected();

    }

    /// <summary>
    /// GridView行数据绑定件事
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
        }

        //这里的处理是为了回显之前选中的情况
        if (e.Row.RowIndex > -1 && this.SelectedItems != null)
        {
            DataRowView row = e.Row.DataItem as DataRowView;
            CheckBox cb = e.Row.FindControl(_CTR_CHECKBOX_PKID) as CheckBox;
            if (this.SelectedItems.Contains(row[_TABLE_FIELD_PKID].ToString()))
                cb.Checked = true;
            else
                cb.Checked = false;

            if (Session[Constants.SESSION_OLD_SELECTED] != null)
            {
                /**------------------maybe start-----------------------------------
                 * 说明：设置载入时文本框不可编辑
                 *       在使用时，如有多个文本框，要选在上面获取
                 */
                
                Hashtable htSelected = Session[Constants.SESSION_OLD_SELECTED] as Hashtable;
                string strPkid = row[_TABLE_FIELD_PKID].ToString();
                //  1、获取文本框
                TextBox txtSource = e.Row.FindControl(_CTR_TEXTBOX_PKID_SOURCE) as TextBox;


                if (htSelected.Contains(strPkid))
                {
                    cb.Checked = true;
                    cb.Enabled = false;
                    //  2、设置文本框值
                    txtSource.Text = (htSelected[strPkid] as DemoChildParams).Source;
                    //  3、设置文本框不可编辑
                    txtSource.Enabled = false;
                }

                /*---------------------maybe end----------------------------------*/
                
            }

        }
    }
    /// <summary>
    /// 提交事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CollectSelected();  //  最后，需要对选中项进行操作之前，不能忘了还要最后一次收集当前页的选中情况

        Hashtable htOldDate = SelectedItems;    //  设置所有已选中的数据

        Hashtable htNewDate = new Hashtable();  //  设置新选中的数据
        //获取新选中的数据
        if (Session[Constants.SESSION_OLD_SELECTED] != null)
        {
            Hashtable htSessionDate = Session[Constants.SESSION_OLD_SELECTED] as Hashtable;
            htNewDate = htOldDate.Clone() as Hashtable;
            foreach (DictionaryEntry de in htSessionDate)
            {
                /**-------------------maybe start-----------------------------
                 *操作：更改对象类型
                 */
                DemoChildParams demoChild = de.Value as DemoChildParams;
                /*-------------------maybe start-----------------------------*/
                string pkid = demoChild.PKID;
                htNewDate.Remove(pkid);
            }
        }
        //  将新选中的数据置入session中
        Session[Constants.SESSION_NEW_SELECTED] = htNewDate;
        //  将所有已选中的数据置入session中
        Session[Constants.SESSION_OLD_SELECTED] = htOldDate;
        ViewState.Clear();
        Response.Write("<script>window.parent.top.opener=null;window.close();</script");
    }
    #endregion

    #region 公共方法
    /// <summary>
    /// 获取或设置选中项的集合
    /// </summary>
    protected Hashtable SelectedItems
    {
        get
        {
            return (ViewState[_VIEWSTATE_NEW_SELECTED] != null) ? (Hashtable)ViewState[_VIEWSTATE_NEW_SELECTED] : null;
        }
        set
        {
            ViewState[_VIEWSTATE_NEW_SELECTED] = value;
        }
    }

    /// <summary>
    /// 从当前页收集选中项的情况
    /// </summary>
    protected void CollectSelected()
    {
        Hashtable selectedItems = null;
        if (this.SelectedItems == null)
            selectedItems = new Hashtable();
        else
            selectedItems = SelectedItems;

        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            /*-------------------maybe start-----------------------------*/
            CheckBox cb = this.GridView1.Rows[i].FindControl(_CTR_CHECKBOX_PKID) as CheckBox;
            HiddenField hd = this.GridView1.Rows[i].FindControl(_CTR_HIDDENFIELD_PKID) as HiddenField;
            //  1、获取文本框对象
            TextBox txtSource = this.GridView1.Rows[i].FindControl(_CTR_TEXTBOX_PKID_SOURCE) as TextBox;
            string id = hd.Value;
            string source = txtSource.Text;
            if (selectedItems.Contains(id) && !cb.Checked)
            {
                selectedItems.Remove(id);
            }
            if (!selectedItems.Contains(id) && cb.Checked)
            {
                //  2、设置参数对象
                DemoChildParams chdParams = new DemoChildParams();
                chdParams.PKID = id;
                //  3、设置文本框的值
                chdParams.Source = source;
                selectedItems.Add(id,chdParams);
            }
            /*---------------------maybe end----------------------------*/
        }
        this.SelectedItems = selectedItems;
    }
    #endregion
}
